﻿/// <reference path="~/Scripts/jquery-1.10.2.intellisense.js" />
/// <reference path="~/Scripts/jquery-ui-1.10.4.js" />

function Users() { }

Users.prototype = {

    getGroups: function () {
        /// <summary>
        ///     送信者履歴コンボボックスのリスト取得
        /// </summary>

        var val = null;
        $.ajaxSetup({ async: false });//同期通信(json取ってくるまで待つ)
        $.ajax({
            type: 'post',
            url: appPath + '/api/Groups',
            cache: false,
            data: null,
            dataType: 'json',
            contentType: "application/json",
            success: function (data, textStatus, jqXHR) {
                val = data;
            },
            error: function (jqXHR, textStatus, errorThrown) {
                var msg;
                if (jqXHR.responseJSON) {
                    msg = jqXHR.responseJSON.ExceptionMessage;
                } else {
                    msg = jqXHR.responseText;
                }
                alert(msg);
            }
        });
        $.ajaxSetup({ async: true });
        if (!val) {
            return [];
        }
        if (val.length == 0) {
            return [];
        }
        return val;
    }

};
var me = new Users;


$(function () {

    $('#txtGroupNew, #txtGroup').selectize({
        delimiter: ',',
        maxItems: null,
        persist: false,
        valueField: 'Id',
        searchField: ['Id', 'Name'],
        options: me.getGroups(),
        render: {
            item: function (item, escape) {
                return '<div>' +
                    (item.Name ? '<span class="name">' + escape(item.Name) + '</span>' : '') +
                    '</div>';
            },
            option: function (item, escape) {
                var label = item.Id;
                var caption = item.Name;
                return '<div>' +
                    '<span class="caption">' + escape(caption) + '</span>' +
                '</div>';
            }
        }
    });

});
